package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcFund;
import com.yxinsur.imc.business.domain.ImcFundBrokerExpression;
import com.yxinsur.imc.business.domain.ImcFundOrderExpression;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.FundForm;
import com.yxinsur.imc.business.form.FundSaveForm;
import com.yxinsur.imc.business.mapper.ImcFundBrokerExpressionMapper;
import com.yxinsur.imc.business.mapper.ImcFundMapper;
import com.yxinsur.imc.business.mapper.ImcFundOrderExpressionMapper;
import com.yxinsur.imc.business.service.IImcFundService;
import com.yxinsur.imc.business.vo.FundVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基本法配置主Service业务层处理
 *
 * @author imc
 * @date 2021-05-25
 */
@Service
public class ImcFundServiceImpl extends ServiceImpl<ImcFundMapper, ImcFund> implements IImcFundService {
    @Autowired
    private ImcFundMapper imcFundMapper;
    @Autowired
    private ImcFundBrokerExpressionMapper fundBrokerExpressionMapper;
    @Autowired
    private ImcFundOrderExpressionMapper fundOrderExpressionMapper;

    /**
     * 查询基本法配置主
     *
     * @param id 基本法配置主ID
     * @return 基本法配置主
     */
    @Override
    public ImcFund selectImcFundById(Long id) {
        return imcFundMapper.selectImcFundById(id);
    }

    /**
     * 查询基本法配置主列表
     *
     * @param fundForm 基本法配置主
     * @return 基本法配置主
     */
    @Override
    public List<FundVo> selectImcFundList(FundForm fundForm) {
        fundForm.setTenantId(SecurityUtils.getTenantId());
        return imcFundMapper.selectImcFundList(fundForm);
    }

    /**
     * 新增基本法配置主
     *
     * @param fundSaveForm 基本法配置主
     * @return 结果
     */
    @Override
    public int insertImcFund(FundSaveForm fundSaveForm) {
        ImcFund imcFund = new ImcFund();
        BeanUtils.copyProperties(fundSaveForm,imcFund);
        imcFund.setCreateTime(DateUtils.getNowDate());
        imcFund.setCreateId(SecurityUtils.getUsername());
        imcFund.setUpdateId(SecurityUtils.getUsername());
        imcFund.setUpdateTime(DateUtils.getNowDate());
        imcFund.setTenantId(SecurityUtils.getTenantId());
        return imcFundMapper.insertImcFund(imcFund);
    }

    /**
     * 修改基本法配置主
     *
     * @param fundSaveForm 基本法配置主
     * @return 结果
     */
    @Override
    public int updateImcFund(FundSaveForm fundSaveForm) {
        ImcFund imcFund = new ImcFund();
        imcFund.setId(fundSaveForm.getFundId());
        BeanUtils.copyProperties(fundSaveForm,imcFund);
        imcFund.setUpdateTime(DateUtils.getNowDate());
        imcFund.setUpdateId(SecurityUtils.getUsername());
        return imcFundMapper.updateImcFund(imcFund);
    }

    /**
     * 批量删除基本法配置主
     *
     * @param ids 需要删除的基本法配置主ID
     * @return 结果
     */
    @Override
    public int deleteImcFundByIds(Long[] ids) {
        //删除子表  主表
        UpdateWrapper<ImcFundBrokerExpression> brokerWrapper = new UpdateWrapper<>();
        brokerWrapper.in("fund_id",ids);
        ImcFundBrokerExpression brokerExpression = new ImcFundBrokerExpression();
        brokerExpression.setIsDeleted(IsDeletedEnum.Y.getCode());
        fundBrokerExpressionMapper.update(brokerExpression,brokerWrapper);
        UpdateWrapper<ImcFundOrderExpression> orderWrapper = new UpdateWrapper<>();
        orderWrapper.in("fund_id",ids);
        ImcFundOrderExpression orderExpression = new ImcFundOrderExpression();
        orderExpression.setIsDeleted(IsDeletedEnum.Y.getCode());
        fundOrderExpressionMapper.update(orderExpression,orderWrapper);

        UpdateWrapper<ImcFund> fundWrapper = new UpdateWrapper<>();
        fundWrapper.in("id",ids);
        ImcFund imcFund = new ImcFund();
        imcFund.setIsDeleted(IsDeletedEnum.Y.getCode());
        return imcFundMapper.update(imcFund,fundWrapper);
    }

    /**
     * 删除基本法配置主信息
     *
     * @param id 基本法配置主ID
     * @return 结果
     */
    @Override
    public int deleteImcFundById(String id) {
        return imcFundMapper.deleteImcFundById(id);
    }
}
